//
//  BillDetailViewController.m
//  Findif
//
//  Created by Mac on 2019/5/7.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "BillDetailViewController.h"
#import "OrderDetailsViewController.h"
#import "GoodsDetailsViewController.h"

@interface BillDetailViewController ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIButton * topButton;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * moneyLabel;
@property (nonatomic, weak)UIView * lineView;

@property (nonatomic, strong)UILabel * exploreLabel;
@property (nonatomic, strong)UILabel * costTimeLabel;
@property (nonatomic, strong)UILabel * orderNumLabel;
@property (nonatomic, strong)UILabel * exploreValueLabel;
@property (nonatomic, strong)UILabel * costTimeValueLabel;
@property (nonatomic, strong)UILabel * orderNumValueLabel;

@property (nonatomic, copy)NSString * goods_order_goods_id;
@property (nonatomic, copy)NSString * goodsType;
@property (nonatomic, copy)NSString * goods_order_id;

@end

@implementation BillDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"账单详情";
    self.view.backgroundColor = ColorWithRGB(0xF8F9FA);
    
    [self loadData];
    [self setupUI];
}


- (void)loadData{

    [[ZRNetworkTool sharedTool] getUserBillInfoWithBill_id:self.ID WithBlock:^(BOOL success, id responseData) {
        
        
        if (success) {
            
            NSDictionary * dict = responseData[@"data"];
            NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"merchants_goods_img"]];
            NSURL * url = [NSURL URLWithString:iconStr];
            [self.iconImageView sd_setImageWithURL:url];
            
            self.goods_order_goods_id = [NSString stringWithFormat:@"%@",dict[@"goods_order_goods_id"]];
            self.goods_order_id = [NSString stringWithFormat:@"%@",dict[@"goods_order_id"]];
            self.nameLabel.text = dict[@"merchants_goods_name"];
            self.exploreValueLabel.text = dict[@"cost_instructions"];
            self.costTimeValueLabel.text = dict[@"inputtime"];
            self.orderNumValueLabel.text = dict[@"goods_order_no"];
            float cost = [dict[@"cost"] floatValue];
            if (cost >= 0) {
                
                self.moneyLabel.textColor = [UIColor redColor];
            }else{
                
                self.moneyLabel.textColor = ZRBlackColor;
            }
            self.moneyLabel.text = [NSString stringWithFormat:@"%@",dict[@"cost"]];
            NSString * goods_order_goods_field = dict[@"goods_order_goods_field"];
            if (goods_order_goods_field.length > 0) {
                
                self.timeLabel.text = dict[@"goods_order_goods_field"];
                self.goodsType = @"2";
            }else{
                
                self.goodsType = @"1";
                self.timeLabel.height = 0;
            }
        }
    }];
}


- (void)setupUI{
    
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(15 * SCREENH6, topHeight + 10 * SCREENH6, 345 * SCREENH6, 340 * SCREENH6)];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    backView.userInteractionEnabled = YES;
    
    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    
    UIImageView * iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(142 * SCREENH6, 20 * SCREENH6, 60 * SCREENH6, 60 * SCREENH6)];
    
    iconImageView.backgroundColor = [UIColor lightGrayColor];
    iconImageView.layer.cornerRadius = 30 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    self.iconImageView = iconImageView;
    
    [self.backView addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * SCREENH6, 95 * SCREENH6, 325 * SCREENH6, 15 * SCREENH6)];
    
    nameLabel.textColor = ZRBlackColor;
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    
    self.nameLabel = nameLabel;
    
    [self.backView addSubview:self.nameLabel];
    
    
    UILabel * timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * SCREENH6, 120 * SCREENH6, 325 * SCREENH6, 13 * SCREENH6)];
    
    timeLabel.text = @"----";
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    
    self.timeLabel = timeLabel;
    
    [self.backView addSubview:self.timeLabel];
    
    
    UILabel * moneyLabel = [UILabel new];
    
    moneyLabel.textColor = [UIColor redColor];
    moneyLabel.font = [UIFont systemFontOfSize:18 * SCREENH6];
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    
    self.moneyLabel = moneyLabel;
    
    [self.backView addSubview:self.moneyLabel];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xE4E4E4);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
    
    self.exploreLabel = [self setupLabelWithTitle:@"账单说明" Type:@"light"];
    [self.backView addSubview:self.exploreLabel];
    self.costTimeLabel = [self setupLabelWithTitle:@"结算时间" Type:@"light"];
    [self.backView addSubview:self.costTimeLabel];
    self.orderNumLabel = [self setupLabelWithTitle:@"订单号" Type:@"light"];
    [self.backView addSubview:self.orderNumLabel];
    
    self.exploreValueLabel = [self setupLabelWithTitle:@"----" Type:@"black"];
    [self.backView addSubview:self.exploreValueLabel];
    self.costTimeValueLabel = [self setupLabelWithTitle:@"----" Type:@"black"];
    [self.backView addSubview:self.costTimeValueLabel];
    self.orderNumValueLabel = [self setupLabelWithTitle:@"----" Type:@"black"];
    self.orderNumValueLabel.userInteractionEnabled = YES;
     UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.orderNumValueLabel addGestureRecognizer:tapGR];
    [self.backView addSubview:self.orderNumValueLabel];
    
    UIButton * topButton = [UIButton new];
    
    [topButton addTarget:self action:@selector(clickTopButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.topButton = topButton;
    
    [self.backView addSubview:self.topButton];
}


-(void)viewDidLayoutSubviews{
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.backView);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-5 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.moneyLabel.mas_bottom).offset(40 * SCREENH6);
    }];
    
    [self.topButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self.backView);
        make.bottom.mas_equalTo(self.lineView.mas_top);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.exploreValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.exploreLabel);
        make.leading.mas_equalTo(self.exploreLabel.mas_trailing).offset(10 * SCREENH6);
    }];
    
    [self.costTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.exploreLabel);
        make.top.mas_equalTo(self.exploreLabel.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.costTimeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.costTimeLabel);
        make.leading.mas_equalTo(self.exploreValueLabel);
    }];
    
    [self.orderNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.costTimeLabel);
        make.top.mas_equalTo(self.costTimeLabel.mas_bottom).offset(20 * SCREENH6);
    }];

    [self.orderNumValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.orderNumLabel);
        make.leading.mas_equalTo(self.costTimeValueLabel);
    }];
}


- (UILabel *)setupLabelWithTitle:(NSString *)title Type:(NSString *)type{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    label.font = [UIFont systemFontOfSize:14 * SCREENH6];
    if ([type isEqualToString:@"light"]) {
        
        label.textColor = [UIColor lightGrayColor];
    }else{
        
        label.textColor = ZRBlackColor;
    }
    
    return label;
}


- (void)clickTopButton{
    
    HiddenBar;
    GoodsDetailsViewController * goodsVC = [GoodsDetailsViewController new];
    
    goodsVC.ID = self.goods_order_goods_id;
    NSLog(@"==%@",self.goodsType);
    
    goodsVC.infoType = self.goodsType;
    
    [self.navigationController pushViewController:goodsVC animated:YES];
}


- (void)tapAction{
    
    HiddenBar;
    OrderDetailsViewController * orderVC = [OrderDetailsViewController new];
    
    orderVC.ID = self.goods_order_id;
    orderVC.from = @"user";
    [self.navigationController pushViewController:orderVC animated:YES];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    NSLog(@"==%@",self.orderNumLabel);
    NSLog(@"Value==%@",self.exploreValueLabel);
}

@end
